package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.product.service.SkuInfoService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.redisson.api.RBloomFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

/**
 * @author 钟涛
 * @version 1.0
 * @description: TODO
 * @date 2021/12/8 18:38
 */
@RestController
@RequestMapping("/admin/product")
public class SkuInfoController {

    @Autowired
    private SkuInfoService skuInfoService;

    @Qualifier("skuBloomFilte")
    @Autowired
    private RBloomFilter<Object> rBloomFilter;

    /**
     * 保存sku信息
     * http://192.168.200.1/admin/product/saveSkuInfo
     */
    @PostMapping("/saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo){
        skuInfoService.saveSkuInfo(skuInfo);
        //添加新的sku时，顺便也添加到布隆中
        rBloomFilter.add(skuInfo.getId().toString());
        return Result.ok();
    }

    /**
     * sku信息的分页显示
     * @param
     * @return
     */
    @GetMapping("/list/{page}/{limit}")
    public Result getSkuPage(@PathVariable("page") Long page,
                             @PathVariable("limit") Long limit){
        Page<SkuInfo> page1 = new Page<>(page,limit);
        IPage<SkuInfo> result = skuInfoService.page(page1);
        return Result.ok(result);
    }

    /**
     * sku上架
     * @return
     */
    @GetMapping("/onSale/{skuId}")
    public Result skuUp(@PathVariable("skuId") Long skuId){
        skuInfoService.updateSkuOnSale(skuId,1);
        return Result.ok();
    }

    /**
     * sku下架
     *
     * @return
     */
    @GetMapping("/cancelSale/{skuId}")
    public Result skDown(@PathVariable("skuId") Long skuId){
        skuInfoService.updateSkuOnSale(skuId,0);
        //添加到
        return Result.ok();
    }
}
